package com.hunglv.transportalert;

import java.util.List;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GpsLoader implements LocationListener {
	private double longitude;
	private double latitude;
	private Context mContext;
	private LocationManager lm;
	List<String> listProviders;

	static final int minTime = 5000;
	static final int minDistance = 5;
	
	public GpsLoader(Context context) {
		mContext = context;
		lm = (LocationManager) mContext
				.getSystemService(Context.LOCATION_SERVICE);
		listProviders = lm.getAllProviders();
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public boolean isLocationProvided() {
		boolean isProvided = false;
		for (String provider : listProviders) {
			if (lm.isProviderEnabled(provider)) {
				isProvided = true;
				break;
			}
		}
		return isProvided;
	}

	public void start() {
		if (isLocationProvided()) {
			for(String provider: listProviders){
				lm.requestLocationUpdates(provider, minTime, minDistance, this);
			}
		}
	}

	public void stop(){
		lm.removeUpdates(this);
	}

	public void onLocationChanged(Location location) {
		if (location != null) {
			setLongitude(location.getLongitude());
			setLatitude(location.getLatitude());
		}
	}

	public void onProviderDisabled(String provider) {
		// Not used
	}

	public void onProviderEnabled(String provider) {
		// Not used
	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// Not used
	}
}
